package com.tangcco.lockwell.controller.student;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.dto.inform.InformDto;
import com.tangcco.lockwell.service.InformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 消息通知
 *
 * @author yanxinping
 * @date 2025-01-14 11:24
 */
@RestController
@RequestMapping("/api/student/inform")
public class InformController {
    @Autowired
    private InformService informService;
    @Autowired
    private WebContext webContext;

    @RequestMapping("/getInformList")
    public RestResponse getInformList(@RequestParam(value = "page", defaultValue = "1") int pageNum,
                                      @RequestParam(value = "size", defaultValue = "10") int size,
                                      @RequestParam(value = "isRead",required = false) Integer isRead) {
        Page page = PageHelper.startPage(pageNum, size);
        page.setOrderBy("i.create_time desc");
        List<InformDto> informList = informService.selectListForStudent(webContext.getCurrentUser().getUserId(),isRead
        );
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(informList);
        return RestResponse.ok(pageInfo);
    }
    @RequestMapping("/getInformListForTeacher")
    public RestResponse getInformListForTeacher(@RequestParam(value = "page", defaultValue = "1") int pageNum,
                                      @RequestParam(value = "size", defaultValue = "10") int size) {
        Page page = PageHelper.startPage(pageNum, size);
        page.setOrderBy("i.create_time desc");
        List<InformDto> informList = informService.selectListForTeacher(webContext.getCurrentUser().getUserId());
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(informList);
        return RestResponse.ok(pageInfo);
    }
    @RequestMapping("/selectDetailForStudent/{informId}")
    public RestResponse<InformDto> selectDetailForStudnt(@PathVariable("informId") Integer informId){
        return RestResponse.ok(informService.selectDetailForStudent(informId));
    }

    @RequestMapping("/markAsRead/{id}")
    public RestResponse markAsRead(@PathVariable("id") int id) {
        informService.markAsRead(id);
        return RestResponse.ok();
    }
    @RequestMapping("/markAllAsRead")
    public RestResponse markAllAsRead() {
        informService.markAllAsReadByUserId(webContext.getCurrentUser().getUserId());
        return RestResponse.ok();
    }

}

